/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller.b;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ykht.proj.admin.api.feign.RemoteUserService;
import com.ykht.proj.admin.api.vo.UserVO;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.UserTypeEnum;
import com.ykht.proj.coupon.entity.*;
import com.ykht.proj.coupon.service.*;
import com.ykht.proj.coupon.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 服务员扩展表
 *
 * @author proj code generator
 * @date 2021-02-01 19:00:18
 */
@RestController
@AllArgsConstructor
@RequestMapping("b/fxwaiterext")
@Api(value = "b/fxwaiterext", tags = "服务员扩展表管理")
public class BxWaiterExtController {

	private final FxWaiterExtService fxWaiterExtService;
	/**
	 * 用户 服务 Api
	 */
	private final RemoteUserService remoteUserService;
	/**
	 * 门店 Api
	 */
	private final CnShopService cnShopService;
	/**
	 * 品牌 Api
	 */
	private final CnBrandService cnBrandService;
	/**
	 * 公司 Api
	 */
	private final CnCompanyService cnCompanyService;
	/**
	 * 服务员奖励记录表
	 */
	private final FxRewardService fxRewardService;
	/**
	 * 提现记录表
	 */
	private final FxWithdrawalService fxWithdrawalService;
	/**
	 * 门店充值记录
	 */
	private final FxShopMoneyFlowService fxShopMoneyFlowService;

	/**
	 * 获取服务员详情
	 *
	 * @return
	 */
	@ApiOperation(value = "获取服务员详情", notes = "获取服务员详情")
	@GetMapping("/getWaiterDetails")
	public R getWaiterDetails() {
		Integer userType = SecurityUtils.getUser().getUserType();
		Long sjId = SecurityUtils.getSjId();
		UserVO user = remoteUserService.getByIdUser(SecurityUtils.getUserId());
		if (userType.equals(UserTypeEnum.WAITER.getValue())) {
			FxWaiterExt waiterExt = fxWaiterExtService.getOne(Wrappers.lambdaQuery(FxWaiterExt.class)
					.eq(FxWaiterExt::getLoginId, SecurityUtils.getUserId())
					.eq(FxWaiterExt::getWorkState, 1).eq(FxWaiterExt::getDelFlag, 0));

			if (waiterExt == null) {
				FxWaiterExtVo fxWaiterExtVo = new FxWaiterExtVo();
				fxWaiterExtVo.setUsername(user.getUsername());
				fxWaiterExtVo.setAvatar(user.getAvatar());
				fxWaiterExtVo.setUserPhone(user.getPhone());
				fxWaiterExtVo.setNickName(user.getNickName());
				return R.ok(fxWaiterExtVo, "你还没用在门店工作呢");
			}
			FxWaiterExtVo fxWaiterExtVo = BeanUtil.toBean(waiterExt, FxWaiterExtVo.class);

			fxWaiterExtVo.setAvatar(user.getAvatar());
			CnShop cnShop = cnShopService.getById(fxWaiterExtVo.getShopId());
			fxWaiterExtVo.setShopName(cnShop.getName());
			CnBrand cnBrand = cnBrandService.getById(fxWaiterExtVo.getBrandId());
			fxWaiterExtVo.setBrandName(cnBrand.getBrandName());
			fxWaiterExtVo.setLogoUrl(cnBrand.getLogoUrl());
			fxWaiterExtVo.setUsername(user.getUsername());
			fxWaiterExtVo.setUserPhone(user.getPhone());
			fxWaiterExtVo.setNickName(user.getNickName());
			fxWaiterExtVo.setAvatar(user.getAvatar());

			// 获取总佣金金额
			FxRewardVo vo = new FxRewardVo();
			List<Long> waiterIdList = fxWaiterExtService.getWaiterIdList();
			vo.setWaiterIdList(waiterIdList);
			vo.setLookRoleType(1);
			vo.setLoginRoleType(3);
			FxRewardVo sumByMoney = fxRewardService.getSumByMoney(vo);
			if (null != sumByMoney && null != sumByMoney.getDistributionTypeMoney()) {
				// 获取已经提现的佣金金额
				FxWithdrawalVo fxWithdrawal = new FxWithdrawalVo();
				fxWithdrawal.setWaiterIdList(waiterIdList);
				FxWithdrawalVo fxWithdrawalVo = fxWithdrawalService.getSumByMoneyCount(fxWithdrawal);
				fxWaiterExtVo.setWithdrawalAmount(sumByMoney.getDistributionTypeMoney());
				if (null != fxWithdrawalVo && null != fxWithdrawalVo.getCumulativeWithdrawalMoney()) {
					fxWaiterExtVo.setWithdrawalAmount(sumByMoney.getDistributionTypeMoney().subtract(fxWithdrawalVo.getCumulativeWithdrawalMoney()));
				}
			}
			return R.ok(fxWaiterExtVo);
		} else if (userType.equals(UserTypeEnum.shop.getValue())) {
			CnShop cnShop = cnShopService.getById(sjId);
			if (cnShop == null) {
				CnShopVo cnShopVo = new CnShopVo();
				cnShopVo.setUsername(user.getUsername());
				cnShopVo.setUserPhone(user.getPhone());
				cnShopVo.setNickName(user.getNickName());
				return R.ok(cnShopVo, "你还没用在门店工作呢");
			}
			FxShopMoneyFlowVo shopBalance = fxShopMoneyFlowService.getShopBalance(cnShop.getId());
			CnShopVo cnShopVo = BeanUtil.toBean(cnShop, CnShopVo.class);
			if (null == shopBalance) {
				shopBalance = new FxShopMoneyFlowVo();
			}
			cnShopVo.setRemainingSum(shopBalance.getRechargeTotal().subtract(shopBalance.getConsumeTotal()));
			cnShopVo.setUsername(user.getUsername());
			cnShopVo.setUserPhone(user.getPhone());
			cnShopVo.setNickName(user.getNickName());
			CnBrand cnBrand = cnBrandService.getById(cnShopVo.getBrandId());
			cnShopVo.setBrandName(cnBrand.getBrandName());
			cnShopVo.setLogoUrl(cnBrand.getLogoUrl());
			CnCompany cnCompany = cnCompanyService.getById(cnShopVo.getCompanyId());
			cnShopVo.setCompanyName(cnCompany.getName());
			return R.ok(cnShopVo);
		} else if (userType.equals(UserTypeEnum.brand.getValue())) {
			CnBrand cnBrand = cnBrandService.getById(sjId);
			if (cnBrand == null) {
				CnBrandVo cnBrandVo = new CnBrandVo();
				cnBrandVo.setUsername(user.getUsername());
				cnBrandVo.setUserPhone(user.getPhone());
				cnBrandVo.setNickName(user.getNickName());
				return R.ok(cnBrandVo, "你还没绑定品牌");
			}
			CnBrandVo cnBrandVo = BeanUtil.toBean(cnBrand, CnBrandVo.class);
			cnBrandVo.setUsername(user.getUsername());
			cnBrandVo.setUserPhone(user.getPhone());
			cnBrandVo.setNickName(user.getNickName());
			CnCompany cnCompany = cnCompanyService.getById(cnBrandVo.getCompanyId());
			cnBrandVo.setCompanyName(cnCompany.getName());
			return R.ok(cnBrandVo);
		} else if (userType.equals(UserTypeEnum.company.getValue())) {
			CnCompany cnCompany = cnCompanyService.getById(sjId);
			if (cnCompany == null) {
				CnCompanyVo cnCompanyVo = new CnCompanyVo();
				cnCompanyVo.setUsername(user.getUsername());
				cnCompanyVo.setUserPhone(user.getPhone());
				cnCompanyVo.setNickName(user.getNickName());
				return R.ok(cnCompanyVo, "你还没绑定公司");
			}
			CnCompanyVo cnCompanyVo = BeanUtil.toBean(cnCompany, CnCompanyVo.class);
			cnCompanyVo.setUsername(user.getUsername());
			cnCompanyVo.setUserPhone(user.getPhone());
			cnCompanyVo.setNickName(user.getNickName());
			return R.ok(cnCompanyVo);
		}
		return R.ok();
	}


	/**
	 * 服务员更换门店或更换品牌
	 *
	 * @return
	 */
	@ApiOperation(value = "服务员更换门店或更换品牌", notes = "服务员更换门店或更换品牌")
	@GetMapping("/replaceShop")
	public R replaceShop(FxWaiterExt fxWaiterExt) {
		return R.ok(fxWaiterExtService.replaceShop(fxWaiterExt));
	}


	/**
	 * 选择0前厅1后厨
	 *
	 * @return
	 */
	@ApiOperation(value = "选择0前厅1后厨", notes = "选择0前厅1后厨")
	@GetMapping("/updateShop")
	public R<FxWaiterExtVo> updateShop(FxWaiterExt fxWaiterExt) {
		fxWaiterExtService.updateById(fxWaiterExt);
		return R.ok();
	}


}
